#include <stack>
#include <vector>

using namespace std;

//非递归
struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
};

class Solution
{
public:
    vector<int> inorderTraversal(TreeNode *root)
    {
        vector<int> ret;
        stack<TreeNode *> st;
        TreeNode *cur = root;
        while (cur || !st.empty())
        {
            while (cur)
            {
                st.push(cur);
                cur = cur->left;
            }

            TreeNode *top = st.top();
            ret.push_back(top->val);
            st.pop();

            cur = top->right;
        }
        return ret;
    }
};